﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Xml.Serialization;
#endregion

namespace AdvanceLib.Core.Configuration
{
    public class Configuration
    {
        #region Constructors
        public Configuration()
        {
            this.Entities = new Dictionary<string, object>();
        }
        #endregion

        #region Fields
        private string _fileName;
        #endregion

        #region Properties
        public string FileName
        {
            get { return this._fileName; }
            set
            {
                this._fileName = value;

                if (File.Exists(this._fileName))
                {
                    try { this.Load(); } catch { }
                }
            }
        }
        public Dictionary<string, object> Entities { get; set; }
        #endregion

        #region Methods
        public void Load()
        {
            this.Load(this.FileName);
        }
        public void Load(string fileName)
        {
            using (FileStream fileStream = new FileStream(fileName, FileMode.Open))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(SerializableDictionary<string, object>));

                SerializableDictionary<string, object> entityDict = (SerializableDictionary<string, object>)serializer.Deserialize(fileStream);
                this.Entities = entityDict.ToDictionary();

                fileStream.Close();
            }
        }
        public void Save()
        {
            this.Save(this.FileName);
        }
        public void Save(string fileName)
        {
            SerializableDictionary<string, object> entityDict = SerializableDictionary<string, object>.FromDictionary(this.Entities);

            using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(SerializableDictionary<string, object>));
                serializer.Serialize(fileStream, entityDict);

                fileStream.Close();
            }
        }
        public void SetEntityByKey(string key, object value)
        {
            if (this.Entities.ContainsKey(key))
                this.Entities[key] = value;
            else
                this.Entities.Add(key, value);
        }
        public object GetEntityByKey(string key)
        {
            if (this.Entities.ContainsKey(key))
                return this.Entities[key];
            else
                return null;
        }
        public T GetEntityValueByKey<T>(string key)
        {
            object entity = this.GetEntityByKey(key);
            return (T)entity;
        }
        public List<object> GetEntityList()
        {
            return this.Entities.Values.ToList();
        }
        #endregion
    }
}
